
	
	
	
	use  MxFLS_final , clear		
	 	

/* select sample   */ 
		
	keep if hhpotentialmigration ==1 
	keep if statusliving05==1 		
	keep if age >14 
	
	
	g young   =  age <=25   
	g old    = age>=26 
	g treat_male= treatment* male
	g treat_female= treatment* female
	g treat_young= treatment* young
	g treat_old= treatment*  old 
	


	
/* indepedent variables   */
	
	#delimit ; 

	global indivlist   female  age age_female   yedu  single   ;  
	global hhlist     		
				ln_socialnonlabincome   						  
				hh_higestgrade3  hh_higestgrade4
				wealthindex  wealthindexsq  
				hhsize     nbage13minus 
				hhrelativeUS hhreturnees
				 ARRIVE_age6less    
				LEAVE_age6less 
	; 
	global shocks dead_0205 disease_0205 job_busi_failure_0205 loss_crop_0205 robb_naturaldis_0205  ///
				  dead_0298  disease_0298 job_busi_failure_0298 loss_crop_0298 robb_naturaldis_0298 ;

	global indep_M2     ${indivlist}   ${hhlist}  ${mun_list2} ${shocks}   ;	
	
	global mig_control hhmexmigrant   hh_newmem_adu  ;
  
	global model_list  M2   ;


	  
/* depedent variables 	*/

		
	global HH_CONSPERCAP   	UnearnedINC0502 food_attana_0502 nonfood_attana_0502  cons_attana_0502    ;  
    global assets_ext  HH_ownfinancialassets0502	HH_ownbicycle0502 	HH_ownmotorvehic0502 	
						 HH_ownelecdevice0502 	HH_ownwashingmachine0502 HH_owndomesappliance0502
						 HH_own_draftanimal0502   HH_own_productanimal0502 HH_ownequipement0502 ;			
	global input_ext     HH_POS_soil_input0502     HH_POS_otherland_input0502  ;
	global input_ext_long       HH_POS_soil_input0902     HH_POS_otherland_input0902  ;
	global input_int_long      HH_soil_input0902     	 HH_otherland_input0902 	;

	global HH_CONSPERCAP_long   cons_precap_0902    cons_rojas_0902 food_attana_0902 nonfood_attana_0902  cons_attana_0902   HH_nbsize0902 HH_nbadult0902   ;  
  
  
	global farmincome	farm_prod_ph_0502 farm_prod_pw_0502 farm_incap_0502 farm_prod_ph_0902 farm_prod_pw_0902 farm_incap_0902 
		HH_incomemonth_self0502 HH_incomemonth_self0902  ; 
	 		
	#delimit cr  ;	
	
	*TRIM AT 1% and 99% for all continous variables
	
	foreach v in   $HH_CONSPERCAP  $HH_CONSPERCAP_long    { 
	
      _pctile `v' , p (1 99)
		replace `v' = . if `v' <= r(r1) |  `v' >= r(r2)
	}
	*


	global option varlabels  tex plain fragment  bdec(3)  se   starloc(1) starlevels(10 5  1) 


					
 *******************************************************************************
	*TABLE S.3 and S.6  – Labor reallocation effects among household members left behind 
 *******************************************************************************
 
  		global labor_par    workLW   workLW_noagrw     workLW_agrw   workLW_self  agriactivity  
		global labor_h      hourLW    hourLW_noagrw  hourLW_agrw    hourLW_self  hoursagriactivity
		global time      	housework 	  care_elderchild  stillinschool  
	  
	foreach LIST in  time     { 
	outreg, clear(A_`LIST') 
		
		global LIS 
		global LIS ${`LIST'}
			
		eststo clear 	
		foreach outcome in $LIS  {	
				
			qui su `outcome'02  if rural==1  & treatment==0 
			local av = round(r(mean), 0.001) 
			
			local a : variable label  `outcome'0502
			qui reg `outcome'0502   treatment  ${mig_control}   ${indivlist}   ${hhlist}   ///
								i.id_mun   if rural==1 , vce(cluster folio)
			outreg, merge(A_`LIST') nod  keep( treatment  )  ctitle(" ", "`a'")  ${option}	 addrows( "2002 average outcome" , `av' )							
			
			} 
					
	}
	*
	
	nois outreg   ,  replay(A_time)  ${option}  title("TABLE S.6  Time use of non-migrant individuals") 

	
	**** Labor supply response depending on age and sex 
		
		foreach LIST in labor_par labor_h {
			outreg, clear(A_`LIST'_y) 
			outreg, clear(A_`LIST'_o) 	
			global LIS 
			global LIS ${`LIST'}	
			eststo clear 	
			foreach outcome in $LIS  {	
				local a : variable label  `outcome'0502
				qui su `outcome'02  if rural==1  & treatment==0  & young==1  
				local av = round(r(mean), 0.001) 
				
				global m1 treat_male treat_female
		
			
				qui reg `outcome'0502   $m1  ${mig_control}   ${indivlist}   ${hhlist}  i.id_mun   if rural==1 & young==1  , vce(cluster folio)
				outreg, merge(A_`LIST'_y) nod  keep(  $m1  )  ctitle(" ", "`a'")  ${option}	 addrows( "2002 average outcome" , `av' )							
			
				qui su `outcome'02  if rural==1  & treatment==0  & young==0  
				local av = round(r(mean), 0.001)
				
				qui reg `outcome'0502   $m1  ${mig_control}   ${indivlist}   ${hhlist}  i.id_mun   if rural==1 & young==0  , vce(cluster folio)
				outreg, merge(A_`LIST'_o) nod  keep(  $m1  )  ctitle(" ", "`a'")  ${option}	 addrows( "2002 average outcome" , `av' )							
				
				
				} 
					
			}
			*
			nois outreg  , replace  replay(A_labor_par_y)   ${option} title(" TABLE S.3 Labor supply of 15-25 non-migrant individuals") 
			nois outreg  , addtable  replay(A_labor_h_y)  ${option} title(" TABLE S.3 Labor supply of 15-25 non-migrant individuals")   
			nois outreg , addtable   replay(A_labor_par_o)   ${option} title("TABLE S.3 Labor supply 26+  of non-migrant individuals") 
			nois outreg , addtable   replay(A_labor_h_o)  ${option} title(" TABLE S.3 Labor supply of 26+non-migrant individuals")   
	
	


*******************************************************************************
	*TABLE S.8 : ROBUSTNESS  of  Labor supply effects  
 *******************************************************************************
 
	
	global option2  varlabels  tex plain fragment summstat(r2)   bdec(5)  se   starloc(1) starlevels(10 5  1) 
	
	   global m1   treatment   
       global m2   treatment i.id_mun
	   global m3   $m2  ${mig_control} ${indivlist}   	hhsize     nbage13minus hh_higestgrade3  hh_higestgrade4 
	   global m4   $m3 ln_socialnonlabincome wealthindex  wealthindexsq    ARRIVE_age6less   LEAVE_age6less 
	   global m5   $m4 hhrelativeUS hhreturnees 
	
		foreach y in  hourLW_self  hoursagriactivity   workLW_self  {
			
			outreg,clear(robust_`y')
	       qui  reg  `y'0502   $m1   if  rural==1 , vce(cluster folio)
		   local R = round(e(r2), 0.0001)
		   outreg, merge(robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R' \ "mun FE", "" \ "demographics" , "" \ "hh wealth and nonlabor income" , "" \ "networks" , ""   )		
		  
		   qui  reg  `y'0502  $m2  if  rural==1 , vce(cluster folio)
		      local R = round(e(r2), 0.0001)
		   outreg, merge( robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R' \ "mun FE", "X" \ "demographics" , "" \ "hh wealth and nonlabor income" , "" \ "networks" , ""    )			
		   
		   qui  reg  `y'0502  $m3  if  rural==1 , vce(cluster folio)
		      local R = round(e(r2), 0.0001)
		   outreg, merge( robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R' \ "mun FE", "X"\ "demographics" , "X" \ "hh wealth and nonlabor income" , "X" \ "networks" , ""    )		
		   
		   qui  reg   `y'0502    $m4  if  rural==1 , vce(cluster folio)
		      local R = round(e(r2), 0.0001)
		   outreg, merge( robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R'\ "mun FE", "X"\ "demographics" , "X" \ "hh wealth and nonlabor income" , "X" \ "networks" , ""    )		
		
		   qui  reg   `y'0502    $m5  if  rural==1 , vce(cluster folio)
		      local R = round(e(r2), 0.0001)
		   outreg, merge( robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R' \ "mun FE", "X" \ "demographics" , "X" \ "hh wealth and nonlabor income" , "X" \ "networks" , "X"   )		
		
		
		   nois outreg, replay( robust_`y') title("TABLE S.8 : ROBUSTNESS  of  Labor supply effects  ") 
		
		}	
		*
  

****************************************************************
	*Household Level Outcome  
 ****************************************************************
 
 	save temp, replace
	
	use temp,clear
 
 
	global variable_hh     hh_newmem_adu HH_work* HH_hour* HH_agri* con* HH_own* UnearnedINC* food_attana_* nonfood_attana_* HH_POS_soil_*  HH_POS_otherland*   HH_fertilizer*   HH_seedpest*   ///
			rural community_type hourLW02   workLW02  hh_self  hh_farm  ${indivlist}   ${hhlist}  ${mun_list2} ${shocks}  ///
			treatment     hhmexmigrant    HH_nb_outwork_business*  HH_nb_outworkers_land*  HH_one_outworker_land*  HH_one_outworker_business* ///
			id_mun   nbmaleage3654 nbmaleage1435 nbage55plus   farm_*  HH_incomemonth_self*  HH_*livestockincome*  HH_*cropsales* HH_*ruralsales* HH_*allfarm*
	
	* collapse data at household level 
	
	qui{	
		cap drop __* 
		foreach v of varlist $variable_hh    {
		local l`v' : variable label `v'
			if `"`l`v''"' == "" {
			local l`v' "`v'"
			di "`v'"
		}
	  }
	  
		collapse (mean)    $variable_hh    , by (folio)

		foreach v of varlist  $variable_hh	{
		label var `v' "`l`v''"
	  }
	 *
	 }
	 *
	save temp_hh, replace
	
		 
	
		global W wealthindex wealthindexsq
		global hhlist2  : list global(hhlist) - global(W)	
		desc $hhlist2
		
	 global assets_ext  HH_ownfinancialassets	HH_ownbicycle 	HH_ownmotorvehic  HH_ownelecdevice 	HH_ownwashingmachine HH_owndomesappliance ///
					    HH_own_draftanimal   HH_own_productanimal HH_ownequipement 	 
    global assets_ext_long 	 ${assets_ext}
	global HH_CONSPERCAP_long   food_attana_ nonfood_attana_  cons_attana_              	
	global outsideworker  HH_one_outworker_land   HH_nb_outworkers_land   HH_one_outworker_business  HH_nb_outwork_business

	
***************************************************************************************************
*TABLE S.1  Effect of migration on farm inputs in the short and long-run
 ***********************************************************************************************************
 
  
	xtile cons2_median= cons_attana_02 if rural==1, nq(2)
	
	g treat_poor= treatment *(cons2_median==1)
	g treat_rich = treatment *(cons2_median==2)
	g poor= cons2_median==1 if rural==1
	label var  treat_poor "US mig*Poor"
	label var  treat_rich "US mig*Rich"
	
	global farminput    HH_fertilizer   HH_seedpest   HH_POS_otherland_input  

		outreg, clear(A_farminput  ) 
		outreg, clear(A_farminput_long) 
			
		foreach outcome in $farminput   {	
			   
			local a : variable label  `outcome'0502
			  
			qui su `outcome'02  if rural==1 & treatment==0 
			local av = round(r(mean), 0.001) 
			
			qui reg `outcome'0502  treatment  ${mig_control}   ${hhlist2} nbmaleage3654 nbmaleage1435 nbage55plus ${shock}  i.id_mun   if  rural==1 	
			outreg, merge(A_farminput) nod  keep( treatment  )  ctitle(" ", "`a'")  ${option} 	addrows( "2002 average outcome" , `av' )

			qui reg `outcome'0502   treat_poor treat_rich poor ${mig_control}   ${hhlist2} nbmaleage3654 nbmaleage1435 nbage55plus ${shock}  i.id_mun   if  rural==1 			
			outreg, merge(A_farminput) nod  keep(treat_poor treat_rich )  ctitle(" ", "`a'")  ${option} 	addrows( "2002 average outcome" , `av' )
			
			local a : variable label  `outcome'0902
			
			qui reg `outcome'0902  treatment  ${mig_control}   ${hhlist2} nbmaleage3654 nbmaleage1435 nbage55plus ${shock}  i.id_mun   if  rural==1 	
			outreg, merge(A_farminput_long) nod  keep( treatment  )  ctitle(" ", "`a'")  ${option} 	addrows( "2002 average outcome" , `av' )

			qui reg `outcome'0902   treat_poor treat_rich poor ${mig_control}   ${hhlist2} nbmaleage3654 nbmaleage1435 nbage55plus ${shock}  i.id_mun   if  rural==1 			
			outreg, merge(A_farminput_long) nod  keep(treat_poor treat_rich )  ctitle(" ", "`a'")  ${option} 	addrows( "2002 average outcome" , `av' )

							
			}
			*	
		nois outreg   ,    replay(A_farminput)  ${option}  title("TABLE S.1  : PANEL A -  household farm inputs ")
		nois outreg   ,    replay(A_farminput_long )  ${option}  title("TABLE S.1  : PANEL B -  household farm inputs ")
	
	
****************************************************************************************************
	*TABLE S.2 : Short- and longer-term effects of U.S. migration on household durable assets and consumption
 ***********************************************************************************************************
 
		
		foreach LIST in assets_ext  outsideworker  { 
			
			outreg, clear(A_`LIST') 
			global LIS 
			global LIS ${`LIST'}		
			foreach outcome in $LIS  {	
				
			qui su `outcome'02   if rural==1 & treatment==0 
			local av = round(r(mean), 0.001) 
			local a : variable label  `outcome'0502
			
			qui  reg `outcome'0502  treatment    ${mig_control}   ${hhlist} nbmaleage3654 nbmaleage1435 nbage55plus  i.id_mun   if  rural==1 , vce(cluster folio)
			
			* exclude wealth index from controls when dependent var = assets*
			if  ( "`LIST'"=="assets_ext" |   "`LIST'"=="assets_int" |   "`LIST'"=="assets_int_long" |   "`LIST'"=="assets_ext_long"  ) {
			 qui reg `outcome'0502   treatment    ${mig_control}   ${hhlist2} nbmaleage3654 nbmaleage1435 nbage55plus   ///
						i.id_mun   if  rural==1  , vce(cluster folio) 
			}			
			outreg, merge(A_`LIST') nod  keep(  treatment     )  ctitle(" ", "`a'")  ${option}		addrows( "2002 average outcome" , `av' )		
			}					
			}
			*
			foreach LIST in assets_ext_long HH_CONSPERCAP_long    { 
			outreg, clear(A_`LIST') 
			global LIS 
			global LIS ${`LIST'}		
			foreach outcome in $LIS  {		
			
			qui su `outcome'02  if rural==1 & treatment==0 
			local av = round(r(mean), 0.001) 
			local a : variable label  `outcome'0902
			
			 qui reg `outcome'0902  treatment    ${mig_control}   ${hhlist2} nbmaleage3654 nbmaleage1435 nbage55plus  ///
						i.id_mun   if  rural==1 			
			outreg, merge(A_`LIST') nod  keep(  treatment     )  ctitle(" ", "`a'")  ${option}		addrows( "2002 average outcome" , `av' )		
			}					
			}
			
			
			nois outreg   ,   replay(A_HH_CONSPERCAP_long)  ${option}  title(" TABLE S.2 : durable consumption between 2002 and 2009 ")
			nois outreg,   replay(A_assets_ext)   ${option}  title("  TABLE S.2  : household assets 2005-2002") 
			nois outreg,   replay( A_assets_ext_long ) ${option}  title("  TABLE S.2 :  household assets 2009 -2002")
			

***************************************************************************************************
*TABLE S.5 – Non-family workers hired in the household farm and business
 ***********************************************************************************************************
 
 
			nois outreg   ,   replay( A_outsideworker )  ${option}  title("TABLE S.5-  Non-family workers hired in the household farm and business  ")

***************************************************************************************************
*TABLE S.8 – Robustness checks :consumtpion effects
***********************************************************************************************************
 
 
 ** Robusteness analysis 
	
	
  		
	   global m1   treatment   
       global m2   treatment i.id_mun
	   global m3   $m2  ${mig_control}  hhsize     nbage13minus hh_higestgrade3  hh_higestgrade4  nbmaleage3654 nbmaleage1435 nbage55plus 
	   global m4   $m3 ln_socialnonlabincome wealthindex  wealthindexsq    ARRIVE_age6less   LEAVE_age6less 
	   global m5   $m4 hhrelativeUS hhreturnees 
	
		
		foreach y in   cons_attana_  {
			
			outreg,clear(robust_`y')
	       qui  reg  `y'0502   $m1   if  rural==1 , vce(cluster folio)
		   local R = round(e(r2), 0.0001)
		   outreg, merge(robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R' \ "mun FE", "" \ "demographics" , "" \ "hh wealth and nonlabor income" , "" \ "networks" , ""   )		
		  
		   qui  reg  `y'0502  $m2  if  rural==1 , vce(cluster folio)
		      local R = round(e(r2), 0.0001)
		   outreg, merge( robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R' \ "mun FE", "X" \ "demographics" , "" \ "hh wealth and nonlabor income" , "" \ "networks" , ""    )			
		   
		   qui  reg  `y'0502  $m3  if  rural==1 , vce(cluster folio)
		      local R = round(e(r2), 0.0001)
		   outreg, merge( robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R' \ "mun FE", "X"\ "demographics" , "X" \ "hh wealth and nonlabor income" , "X" \ "networks" , ""    )		
		   
		   qui  reg   `y'0502    $m4  if  rural==1 , vce(cluster folio)
		      local R = round(e(r2), 0.0001)
		   outreg, merge( robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R'\ "mun FE", "X"\ "demographics" , "X" \ "hh wealth and nonlabor income" , "X" \ "networks" , ""    )		
		
		   qui  reg   `y'0502    $m5  if  rural==1 , vce(cluster folio)
		      local R = round(e(r2), 0.0001)
		   outreg, merge( robust_`y') nod  keep(treatment)  ${option} addrows("R2", `R' \ "mun FE", "X" \ "demographics" , "X" \ "hh wealth and nonlabor income" , "X" \ "networks" , "X"   )		
		
		
		   nois outreg, replay( robust_`y') title("TABLE S.8 – Robustness checks of consumption effects")
		
		}	
		*
  		 
	
	
	
	
***************************************************************************************************
*TABLE S.4 – The impact of migration on the education of those left behind
***********************************************************************************************************
 
 

	use  MxFLS_final , clear		
		  	   
/* select sub sample   */ 
		
		keep if hhpotentialmigration ==1 
		keep if statusliving05==1 		
		keep if age >5  & age<26
	
	g yedu0502= yedu05- yedu02
	label var yedu0502 "years edu 05-02" 	

	g treat_male= treatment* male
	g treat_female= treatment* female

	g a1520 = age>=15  & age <=25 
	g a14 =  age >=6 &  age <=14  

	global edu yedu stillinschool  


	outreg, clear(A_edu_1) 
	outreg, clear(A_edu_2) 
	eststo clear 	
	foreach outcome in $edu  {	

		qui su `outcome'02  if rural==1  & treatment==0 & a14==1
		local av = round(r(mean), 0.001) 

		qui reg `outcome'0502   treat_male treat_female  ${mig_control}   ${indivlist}   ${hhlist}  i.id_mun  if rural==1 &  a14==1  , vce(cluster folio)
		outreg, merge(A_edu_1)nod  keep( treat_male treat_female  )  ctitle("age 6-14 ", "`a'")  ${option}	 addrows( "2002 average outcome" , `av' )							

		qui su `outcome'02  if rural==1  & treatment==0 & a1520==1 ==1
		local av = round(r(mean), 0.001) 

		qui reg `outcome'0502   treat_male treat_female  ${mig_control}   ${indivlist}   ${hhlist}  i.id_mun  if rural==1 &  a1520==1  , vce(cluster folio)
		outreg, merge(A_edu_2)nod  keep( treat_male treat_female  )  ctitle("age 15-25 ", "`a'")  ${option}	 addrows( "2002 average outcome" , `av' )							

	} 
		
	nois outreg  , addtable  replay(A_edu_1)   ${option} title("TABLE S.4 – The impact of migration on the education of those left behind")
	nois outreg  , addtable   replay(A_edu_2)   ${option} title("TABLE S.4 – The impact of migration on the education of those left behind")




		
	
